package com.handong.handserialprogram.module.createmission.recordtask;

import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.constants.ColorContant;
import com.handong.handserialprogram.model.entities.PersonEntity;

import java.util.List;

/**
 * Created by userKey on 2018/2/2.
 */

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchHolder> {

    private List<PersonEntity> mPersonList;
    private AddListener mListener;
    private boolean mIsCriminal;

    @Override
    public int getItemCount() {
        return mPersonList == null ? 0 : mPersonList.size();
    }

    public SearchAdapter(List<PersonEntity> list, AddListener listener, boolean isCriminal) {
        mPersonList = list;
        mListener = listener;
        mIsCriminal = isCriminal;
    }

    @Override
    public SearchHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_search_result, parent, false);
        return new SearchHolder(rootView);
    }

    @Override
    public void onBindViewHolder(SearchHolder holder, final int position) {
        final PersonEntity entity = mPersonList.get(position);
        holder.name.setText(entity.getName());
        holder.id.setText(entity.getId());
        holder.icon.setImageResource(mIsCriminal ? R.drawable.icon_criminal_photo : R.drawable.icon_police_photo);
        GradientDrawable drawable = (GradientDrawable) holder.add.getBackground();
        if (entity.isChecked()) {
            holder.add.setText("已添加");
            drawable.setColor(ColorContant.MISSION_DOING);
            holder.add.setOnClickListener(null);
        } else {
            holder.add.setText("添加");
            drawable.setColor(ColorContant.OFFLINE_CITY_DOWNLOADING);
            holder.add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) mListener.addSearchPerson(entity, position);
                }
            });
        }

    }

    class SearchHolder extends RecyclerView.ViewHolder {

        private ImageView icon;
        private TextView name, id, add;

        public SearchHolder(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.choosePersonIcon);
            name = (TextView) itemView.findViewById(R.id.choosePersonName);
            id = (TextView) itemView.findViewById(R.id.choosePersonId);
            add = (TextView) itemView.findViewById(R.id.addSearchPerson);
        }
    }

    interface AddListener {
        void addSearchPerson(PersonEntity entity, int position);
    }
}
